/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ranger.entity;

/**
 * Base JPA class with id, versionNumber and other common attributes
 */

import org.apache.ranger.common.AppConstants;
import org.apache.ranger.common.DateUtil;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import java.util.Date;
import java.util.Objects;

@MappedSuperclass
@EntityListeners(org.apache.ranger.common.db.JPABeanCallbacks.class)
public abstract class XXDBBase implements java.io.Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * Id for the object
     * <ul>
     * <li>This attribute is the <b>Primary Key</b> for this class<br>.
     * </ul>
     *
     */

    /**
     * Date/Time creation of this user.
     * <ul>
     * </ul>
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_TIME")
    protected Date createTime = DateUtil.getUTCDate();

    /**
     * Date value.
     * <ul>
     * </ul>
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATE_TIME")
    protected Date updateTime = DateUtil.getUTCDate();

    /**
     * Added by
     * <ul>
     * </ul>
     */
    @Column(name = "ADDED_BY_ID")
    protected Long addedByUserId;

    /**
     * Last updated by
     * <ul>
     * </ul>
     */
    @Column(name = "UPD_BY_ID")
    protected Long updatedByUserId;

    /**
     * Default constructor. This will set all the attributes to default value.
     */
    public XXDBBase() {
    }

    public static String getEnumName(String fieldName) {
        //Later TODO
        //return super.getEnumName(fieldName);
        return null;
    }

    public static boolean equals(Object object1, Object object2) {
        if (object1 == object2) {
            return true;
        }

        if ((object1 == null) || (object2 == null)) {
            return false;
        }

        return object1.equals(object2);
    }

    public int getMyClassType() {
        return AppConstants.CLASS_TYPE_NONE;
    }

    public String getMyDisplayValue() {
        return null;
    }

    /**
     * Returns the value for the member attribute <b>id</b>
     *
     * @return Long - value of member attribute <b>id</b>.
     */
    public abstract Long getId();

    /**
     * This method sets the value to the member attribute <b>id</b>.
     * You cannot set null to the attribute.
     *
     * @param id Value to set member attribute <b>id</b>
     */
    public abstract void setId(Long id);

    /**
     * Returns the value for the member attribute <b>createTime</b>
     *
     * @return Date - value of member attribute <b>createTime</b>.
     */
    public Date getCreateTime() {
        return this.createTime;
    }

    /**
     * This method sets the value to the member attribute <b>createTime</b>.
     * You cannot set null to the attribute.
     *
     * @param createTime Value to set member attribute <b>createTime</b>
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * Returns the value for the member attribute <b>updateTime</b>
     *
     * @return Date - value of member attribute <b>updateTime</b>.
     */
    public Date getUpdateTime() {
        return this.updateTime;
    }

    /**
     * This method sets the value to the member attribute <b>updateTime</b>.
     * You cannot set null to the attribute.
     *
     * @param updateTime Value to set member attribute <b>updateTime</b>
     */
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * Returns the value for the member attribute <b>addedByUserId</b>
     *
     * @return Long - value of member attribute <b>addedByUserId</b>.
     */
    public Long getAddedByUserId() {
        return this.addedByUserId;
    }

    /**
     * This method sets the value to the member attribute <b>addedByUserId</b>.
     * You cannot set null to the attribute.
     *
     * @param addedByUserId Value to set member attribute <b>addedByUserId</b>
     */
    public void setAddedByUserId(Long addedByUserId) {
        this.addedByUserId = addedByUserId;
    }

    /**
     * Returns the value for the member attribute <b>updatedByUserId</b>
     *
     * @return Long - value of member attribute <b>updatedByUserId</b>.
     */
    public Long getUpdatedByUserId() {
        return this.updatedByUserId;
    }

    /**
     * This method sets the value to the member attribute <b>updatedByUserId</b>.
     * You cannot set null to the attribute.
     *
     * @param updatedByUserId Value to set member attribute <b>updatedByUserId</b>
     */
    public void setUpdatedByUserId(Long updatedByUserId) {
        this.updatedByUserId = updatedByUserId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(createTime, updateTime, addedByUserId, updatedByUserId);
    }

    /**
     * Checks for all attributes except referenced db objects
     *
     * @return true if all attributes match
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (this.getClass() != obj.getClass()) {
            return false;
        }

        XXDBBase other = (XXDBBase) obj;

        return Objects.equals(createTime, other.createTime) &&
                Objects.equals(updateTime, other.updateTime) &&
                Objects.equals(addedByUserId, other.addedByUserId) &&
                Objects.equals(updatedByUserId, other.updatedByUserId);
    }

    /**
     * This return the bean content in string format
     *
     * @return formatedStr
     */
    @Override
    public String toString() {
        String str = "XXDBBase={";
        //`str += "id={" + id + "} ";
        str += "createTime={" + createTime + "} ";
        str += "updateTime={" + updateTime + "} ";
        str += "addedByUserId={" + addedByUserId + "} ";
        str += "updatedByUserId={" + updatedByUserId + "} ";
        str += "}";
        return str;
    }
}
